package com.jml.produce;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
@Slf4j
public class SyncProducer {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    public TransactionSendResult sendSyncMessage(String msg, String topic, String tag) {
        log.info("【发送消息】：{}", msg);
        Message<String> message = MessageBuilder.withPayload(msg).build();
        TransactionSendResult result = rocketMQTemplate.sendMessageInTransaction
                ("spring_boot_producer_group", topic, message, null);
        log.info("【发送状态】：{}", result.getLocalTransactionState());
        return result;
    }
}
